/**
 * Copyright (C) 2014-2015 5WeHealth Technologies. All rights reserved.
 *  
 *    @author: Jingtao Yun 2012-3-21
 */

package com.wehealth.electrocardiograph_two_client.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import java.util.Iterator;
import java.util.LinkedList;

public class LinePlayView extends View {
	private static String TAG = "LineView";
	private int queueSize = 100;
	//private float ratio = 1;
	private int MaxValue;  //绘制数据最大值
	private int BaseLine;  //中间值
	private int WaveGain;  //波形增益
	private String leadName = new String();
	private LinkedList<Point> mListPoint = new LinkedList<Point>();
	private int xStep;

	Paint mPaint = new Paint();
	Paint wgPaint = new Paint();

	public LinePlayView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		int width = wm.getDefaultDisplay().getWidth();
		Log.e("Width", String.valueOf(width));
//		if (width > 0) {
//			queueSize = width - 200;
//		}
		queueSize = width;
		xStep = 0;
	}

	public LinePlayView(Context context, AttributeSet attrs) {
		super(context, attrs);
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		int width = wm.getDefaultDisplay().getWidth();
		Log.e("Width", String.valueOf(width));
//		if (width > 0) {
//			queueSize = width - 200;
//		}
		queueSize = width;
		xStep = 0;
	}

	public LinePlayView(Context context) {
		super(context);
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		int width = wm.getDefaultDisplay().getWidth();
		Log.e("Width", String.valueOf(width));
//		if (width > 0) {
//			queueSize = width - 200;
//		}
		queueSize = width;
		xStep = 0;
	}
	
	public void setDrawMaxValue(int value){
		MaxValue = value;
	}
	
	public void setBaseLine(int value){
		BaseLine = value;
	}
	
	public void setWaveGain(int value){
		WaveGain = value;
	}
	
	public void setLineViewLeadName(String name)
	{
		leadName = name;
	}
	
	 private int changeToScreenPosition(int data) {
		    int scrheight = this.getMeasuredHeight(); 
		    
	     	return ((scrheight - ((data  - BaseLine) * scrheight * WaveGain) / MaxValue / 100) - (scrheight / 2));
	 }

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		wgPaint.setColor(Color.YELLOW);
		wgPaint.setStrokeJoin(Paint.Join.ROUND);
		wgPaint.setStrokeCap(Paint.Cap.ROUND);
		wgPaint.setStrokeWidth(3);

		long startTime = System.currentTimeMillis();
		
		canvas.drawBitmap(
				drawBackground(30, this.getMeasuredHeight(),
						this.getMeasuredWidth()), 0, 0, wgPaint);
		long end = (System.currentTimeMillis() - startTime);
		//Log.d(TAG, "it took [" + end + "] ms to finish draw background");

		// }
		mPaint.setColor(Color.GREEN);
		mPaint.setAntiAlias(true);

		// for (int index=0; index<mListPoint.size(); index++) {
		// if (index > 0) {
		// canvas.drawLine(mListPoint.get(index-1).x, mListPoint.get(index-1).y,
		// mListPoint.get(index).x, mListPoint.get(index).y, mPaint);
		// canvas.setDrawFilter(new PaintFlagsDrawFilter(0,
		// Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));
		// }
		// }
		
		//Log.e(TAG, String.valueOf(this.getMeasuredWidth()) + String.valueOf(this.getMeasuredHeight()));

		int size = mListPoint.size();
		if (size >= 2) {
			startTime = System.currentTimeMillis();
			Iterator it = mListPoint.iterator();
			Point lastPoint = null;
			while (it.hasNext()) {
				Point currentPoint = (Point) it.next();
				if (lastPoint != null && currentPoint != null && currentPoint.x !=0) {
					canvas.drawLine(lastPoint.x, lastPoint.y,
							currentPoint.x, currentPoint.y, mPaint);
					canvas.setDrawFilter(new PaintFlagsDrawFilter(0,
							Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
				}
				lastPoint = currentPoint;
			}
			canvas.translate(1, 0);
		}

		end = (System.currentTimeMillis() - startTime);
		//Log.d(TAG, "it took [" + end + "] ms to finish draw line");

	}

	/**
	 * 
	 * @param cellSize
	 * @param height
	 * @param widht
	 * @return
	 */
	public  Bitmap drawBackground(int cellSize, int height, int widht) {

		int tmpSize;
		Bitmap bitmap = Bitmap.createBitmap(widht, height, Config.ARGB_8888);
		Canvas cv = new Canvas(bitmap);
		Paint background = new Paint();
		background.setColor(0xff1e2331);
	
		cv.drawRect(0, 0, widht, height, background);
		background.setAntiAlias(true);
	
		Paint rect = new Paint();
		rect.setStyle(Style.STROKE);
		rect.setStrokeWidth(4);
		rect.setColor(0xff303749);
		RectF outerRect = new RectF(4, 4, widht - 4, height - 4);
        cv.drawRoundRect(outerRect, 10, 10, rect);
		
    	background.setColor(0xff303749);
		tmpSize = (cellSize / 5);
		for(int i = 1; i < widht / tmpSize; i++){
			cv.drawLine(tmpSize * i, 5, tmpSize * i , height - 5, background);
		}
		for(int i = 1; i < height / tmpSize; i++){
			cv.drawLine(5, tmpSize * i, widht - 5, tmpSize * i, background);
		}
		
	
		background.setStrokeWidth(3);
		for (int i = 1; i < (widht / cellSize + 1); i++) {
			cv.drawLine(cellSize * i, 5, cellSize * i, height - 5 , background);
		}
		for (int i = 1; i < height / cellSize; i++) {
			cv.drawLine(5, cellSize * i, widht - 5, cellSize * i, background);
		}
		
		//波形名称
		Paint textPaint = new Paint();
		textPaint.setColor(Color.WHITE);
		textPaint.setTextSize(30);
		cv.drawText(leadName, 20, 40, textPaint);
         
         
		return bitmap;
	}

	/**
	 * 往点的集合里添加一个点
	 * 
	 * @param curX
	 *            x轴坐标
	 * @param curY
	 *            y轴坐标
	 */
	public void setLinePoint(int curX, int curY) {
		int x = curX;
		int y = changeToScreenPosition(curY);
		
		xStep++;
		if(xStep >= queueSize)
			xStep = 0;
		x = xStep;
		Point p = new Point(x, y);
	
		int currentSize = mListPoint.size();
		if (currentSize >= (queueSize - 4)) {
			mListPoint.removeFirst();
		}
		Log.i(TAG, "Y值："+curY);
		mListPoint.addLast(p);
		invalidate(); // 刷新视图
	}
	
	/**
	 * 往点的集合里添加一个点
	 * 
	 * @param
	 *
	 * @param curY
	 *            y轴坐标
	 */
	public void setLinePoint(int curY) {
		int x;
		int y = changeToScreenPosition(curY);
		
		xStep++;
		if(xStep >= queueSize)
			xStep = 0;
		x = xStep;
		Point p = new Point(x, y);
	
		int currentSize = mListPoint.size();
		if (currentSize >= (queueSize - 4)) {
			mListPoint.removeFirst();
		}
		Log.i(TAG, "Y值："+curY);
		mListPoint.addLast(p);
		invalidate(); // 刷新视图
	}
	
	public void clearView(){
		mListPoint.clear();
		invalidate();
	}

	public int getQueueSize() {
		return queueSize;
	}

	public void setQueueSize(int queueSize) {
		this.queueSize = queueSize;
	}

}
